package com.virgo.demo.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.cms.converter.ArticleListConverter;
import com.virgo.demo.cms.domain.ArticleCateDo;
import com.virgo.demo.cms.domain.ArticleContentDo;
import com.virgo.demo.cms.domain.ArticleDo;
import com.virgo.demo.cms.domain.bo.ArticleBo;
import com.virgo.demo.cms.domain.vo.ArticleCateVo;
import com.virgo.demo.cms.domain.vo.ArticleInfoVo;
import com.virgo.demo.cms.domain.vo.ArticleListVo;
import com.virgo.demo.cms.mapper.ArticleContentMapper;
import com.virgo.demo.cms.mapper.ArticleMapper;
import com.virgo.demo.cms.service.ArticleService;
import com.virgo.demo.support.service.AppService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleDo> implements ArticleService {

	private final ArticleMapper articleMapper;

	private final ArticleContentMapper articleContentMapper;

	private final ArticleListConverter articleListConverter;

    private final AppService appService;

	/**
	 * 编辑文章
	 * @param articleBo ArticleBo
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void saveArticle(ArticleBo articleBo) {
		ArticleDo articleDo = new ArticleDo();
		BeanUtils.copyProperties(articleBo,articleDo);
		if (ObjectUtils.isEmpty(articleDo) || articleDo.getId().compareTo(0L) < 1) {
			save(articleDo);
		} else {
			updateById(articleDo);
		}
		ArticleContentDo articleContentDo = articleContentMapper.selectById(articleDo.getId());
		if (ObjectUtils.isEmpty(articleContentDo)) {
			articleContentDo = new ArticleContentDo();
			articleContentDo.setId(articleDo.getId());
			articleContentDo.setContent(articleBo.getContent());
			articleContentMapper.insert(articleContentDo);
		} else {
			articleContentDo.setContent(articleBo.getContent());
			articleContentMapper.updateById(articleContentDo);
		}
	}

	/**
	 * 修改状态
	 * @param id     Long
	 * @param status Long
	 */
	@Override
	public void updateStatus(Long id, Integer status) {
		ArticleDo articleDo = new ArticleDo();
		articleDo.setId(id);
		articleDo.setStatus(status);
		updateById(articleDo);
	}

	/**
	 * 删除文章
	 * @param ids String
	 */
	@Override
	public void deleteArticle(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
		removeByIds(idList);
		articleContentMapper.deleteByIds(idList);
	}

	/**
	 * 文章详情
	 * @param id Long
	 * @return ArticleInfoVo
	 */
	@Override
	public ArticleInfoVo findInfo(Long id) {
		ArticleDo articleDo = getById(id);
		if (ObjectUtils.isEmpty(articleDo)) {
			throw new IApplicationException("数据不存在");
		}
		ArticleInfoVo articleInfoVo = new ArticleInfoVo();
		BeanUtils.copyProperties(articleDo,articleInfoVo);
		articleInfoVo.setContent("");
		ArticleContentDo articleContentDo = articleContentMapper.selectById(id);
		if (!ObjectUtils.isEmpty(articleContentDo)) {
			articleInfoVo.setContent(articleContentDo.getContent());
		}
        articleInfoVo.setImageShow(appService.getStaticUrl(articleDo.getImg()));
		return articleInfoVo;
	}

	/**
	 * 文章列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<ArticleListVo>
	 */
	@Override
	public ListDataVo<ArticleListVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<ArticleDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(ArticleDo::getId);

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(ArticleDo::getName,name);
		}

		String cateCode = IRequestUtils.getString(request,"cateCode","");
		if (!ObjectUtils.isEmpty(cateCode)) {
			queryWrapper.like(ArticleDo::getCateCode,cateCode);
		}

		Page<ArticleDo> page = new Page<>(currentPage, pageSize);
		IPage<ArticleDo> iPage = articleMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),articleListConverter.convert(iPage.getRecords(), ArticleListVo.class));
	}
}